﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using AIniciacao.Lib.Screens;
using AIniciacao.Lib.CutScenes;
using AIniciacao.Lib.GameUtils;
using AIniciacao.Lib.Resources;

namespace AIniciacao.Screens
{
    public class CutSceneScreen :GameScreen
    {
        private String text;
        private TimeSpan myTimeSpan;
        private Vector2 position;
        private bool fadeFinished = false;
        private bool topReached = false;
        private byte alphaValue = 254;
        private Color tintColor;
        private Texture2D background;
        private Rectangle bgRect;
        private ContentManager content;
        private CutScene cutScene;
        private static int bestFeetPositionXEnd;
        private const int bestFeetPositionXInit= 30;
        private List<String> linesToWrite;
        
        
        public CutSceneScreen(CutScene cutScene,Color color)
        {
            TransitionOnTime = TimeSpan.FromSeconds(1.5);
            TransitionOffTime = TimeSpan.FromSeconds(0.5);
            
            this.cutScene = cutScene;
            this.tintColor = color;
        }
        public override void LoadContent()
        {
            if (content == null)
                content = new ContentManager(ScreenManager.Game.Services, "Content");
            background = content.Load<Texture2D>(cutScene.backgroundAssetPath);
            text = cutScene.bodyMessageProperty;
            Viewport viewport = ScreenManager.GraphicsDevice.Viewport;
            position = new Vector2(0, ScreenManager.Game.Window.ClientBounds.Height);
            bestFeetPositionXEnd = ScreenManager.Game.Window.ClientBounds.Width - bestFeetPositionXInit;

        }
        public bool IntroFinished
        {
            get { return fadeFinished; }
        }

        /// <summary>
        /// Updates the loading screen.
        /// </summary>
        public override void Update(GameTime gameTime, bool otherScreenHasFocus,
                                                       bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
            myTimeSpan += gameTime.ElapsedGameTime;
            if (myTimeSpan > TimeSpan.FromMilliseconds(25) && !topReached)
            {
                position.Y -= 2.0f;
                myTimeSpan -= TimeSpan.FromMilliseconds(cutScene.delay);
                if (position.Y < 0.0f)
                {
                    position.Y = 0.0f;
                    topReached = true;
                }
            }
            if (myTimeSpan > TimeSpan.FromMilliseconds(15) && topReached)
            {
                if (alphaValue > 0)
                    alphaValue--;
                else
                    fadeFinished = true;
                tintColor.A = alphaValue;
                myTimeSpan -= TimeSpan.FromMilliseconds(65);
            }
            
        }

        public override void Draw(GameTime gameTime)
        {
            if (IsActive)
            {
                SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
                ScreenManager.FadeBackBufferToBlack(TransitionAlpha * 2 / 3);
               
                spriteBatch.Begin();
                bgRect = new Rectangle(0,
                                0,
                                ScreenManager.Game.Window.ClientBounds.Width,
                                ScreenManager.Game.Window.ClientBounds.Height);

                String message = AIniciacaoResourceManager.getProperty(cutScene.bodyMessageProperty);
               
                spriteBatch.Draw(background, bgRect, Color.White);
                
                float firstLinePosY =  position.Y;
                if (linesToWrite == null)
                    linesToWrite = DialogUtils.BuildMessageLines(ScreenManager.Font, message, bestFeetPositionXEnd);
                foreach (string lineToWrite in  linesToWrite)
                {
                    spriteBatch.DrawString(ScreenManager.Font, lineToWrite, new Vector2(bestFeetPositionXInit,firstLinePosY), tintColor);
                    firstLinePosY += ScreenManager.Font.MeasureString(lineToWrite).Y;
                }
               
                spriteBatch.End();
            }
        }
    }
}
